package com.yanqu.road.dao.impl.union.cross;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.dto.CrossPatronsSkill;
import com.yanqu.road.entity.union.cross.CrossUnionChamberPatrons;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionChamberPatronsDaoImpl extends TempDataObjectDao<CrossUnionChamberPatrons> {

    public static String getCrossChamberPatronsSql = "select * from `t_c_union_chamber`";
    public Map<Long, CrossUnionChamberPatrons> getCrossChamberPatrons() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossUnionChamberPatrons> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossChamberPatronsSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionChamberPatrons data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertCrossChamberPatronsSql = "insert into `t_c_union_chamber`(userId,patronsId,skillList,ability,level,stageLv,breakLv,skinId,promotionId) values (?,?,?,?,?,?,?,?,?)";
    
    @Override
    protected boolean addData(CrossUnionChamberPatrons data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSkillListStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStageLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBreakLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSkinId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPromotionId()));
        if (getDb().executeNoneQuery(insertCrossChamberPatronsSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossChamberPatronsSql = "update `t_c_union_chamber` set patronsId=?,skillList=?,ability=?,level=?,stageLv=?,breakLv=?,skinId=?,promotionId=? where userId=?";
    
    @Override
    protected boolean updateData(CrossUnionChamberPatrons data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSkillListStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStageLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBreakLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSkinId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPromotionId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateCrossChamberPatronsSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossUnionChamberPatrons parseData(ResultSet rs) throws SQLException {
        CrossUnionChamberPatrons data = new CrossUnionChamberPatrons();
        data.setUserId(rs.getLong("userId"));
        data.setPatronsId(rs.getInt("patronsId"));
        String str = rs.getString("skillList");
        if(!StringUtils.isNullOrEmpty(str)){
            List<String> skillList = StringUtils.stringToStringList(str, ";");
            for (String skill : skillList) {
                List<Integer> list = StringUtils.stringToIntegerList(skill, ",");
                if(list.size() == 4){
                    CrossPatronsSkill patronsSkill = new CrossPatronsSkill();
                    patronsSkill.setId(list.get(0));
                    patronsSkill.setLv(list.get(1));
                    patronsSkill.setType(list.get(2));
                    patronsSkill.setSkinLv(list.get(3));
                    data.getSkillList().add(patronsSkill);
                }
            }
        }
        data.setAbility(new BigInteger(rs.getString("ability")));
        data.setLevel(rs.getInt("level"));
        data.setStageLv(rs.getInt("stageLv"));
        data.setBreakLv(rs.getInt("breakLv"));
        data.setSkinId(rs.getInt("skinId"));
        data.setPromotionId(rs.getInt("promotionId"));

        return data;
    }
}
